function Grid(dataList, gridContainer, gridTemplate){
	this.dataList = dataList;
	this.gridContainer = gridContainer;
	this.gridTemplate = gridTemplate;
	this.selectedGridRow = null;
	
	//this.form = form;
	
	//this.components = new Array();
	this.gridRows = new Array();
	
	this.onGridRowRender = function(gridRow){};
	this.onGridRowSelect = function(gridRow){};
	this.modelObject = null;
	
	
	return this;
}

//-------------------------------------------------------------------------------------------------

//Grid.prototype.addComponent = function(component){
//	this.components.push(component);
//};

//-------------------------------------------------------------------------------------------------

Grid.prototype.deselectGridRow = function(){
	for (var i=0; i<this.gridRows.length; i++){
		if (this.gridRows[i].isSelected){
			this.gridRows[i].deselect();
			return this.gridRows[i];
		}
	}
	
	return null;
};

//-------------------------------------------------------------------------------------------------

Grid.prototype.renderGrid = function(){
	var grid = this;
	
	this.clearGrid();
	
	for (var i=0; i< this.dataList.length; i++){
		var modelObject = this.dataList[i];
		var gridRow = new GridRow(this.gridTemplate, modelObject);
		gridRow.selectedIndex = i;
		this.onGridRowRender(gridRow);
		
		gridRow.onBeforeSelect = function(){
			grid.deselectGridRow();
			grid.selectedGridRow = null;
		};
		
		gridRow.onSelect = function(){
			grid.modelObject = this.modelObject;
			grid.selectedGridRow = this;
			grid.onGridRowSelect(this);
		};
		
		this.gridRows.push(gridRow);
		this.gridContainer.appendChild(gridRow.renderGridRow());
	}
};

//-------------------------------------------------------------------------------------------------

Grid.prototype.clearGrid = function(){
	if (this.gridContainer.childNodes.length>0){
		for (var i=this.gridContainer.childNodes.length-1;; i--){
			this.gridContainer.removeChild(this.gridContainer.childNodes[i]);
			if (i==0){
				break;
			}
		}
	}
	
};

//-------------------------------------------------------------------------------------------------

Grid.prototype.getModelObject = function(){
	return this.modelObject;
};

//-------------------------------------------------------------------------------------------------

Grid.prototype.setDataList = function(dataList) {
	this.dataList = null;
	
	this.dataList = dataList;
	this.gridRows = new Array();
};